<style lang="scss" scoped>
.cart{
    min-height: calc(100vh - 55px);
    background-color: #f5f5f5;
    .list{
        height: calc(100% - 50px);
        overflow-y: auto;
        padding-bottom: 55px;
        .item{
            background-color: #fff;
            padding: 5px 5%;
            min-height: 120px;
            margin-top: 20px;
            display: flex;
            .radio{
                width: 20px;
                height: 120px;
                display: flex;
                align-items: center;
            }
            .headImg{
                width: 90px;
                height: 120px;
                display: flex;
                align-items: center;
                img{
                    width: 100%;
                    height: 90px;
                }
            }
            .detail{
                width: calc(100% - 160px);
                padding-left: 30px;
                .title{
                    height: 40px;
                    margin-bottom: 10px;
                    line-height: 20px;
                    padding-top: 15px;
                    font-size: 14px;
                    font-weight: bold;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                }
                .sku{
                    display: flex;
                    justify-content: space-between;
                    line-height: 25px;
                    .sku_item{
                        display: inline-block;
                        max-width: 150px;
                        height: 25px;
                        overflow: hidden;
                    }
                    .price{
                        font-size: 14px;
                    }
                }
                .tabbar{
                    width: 100%;
                    text-align: right;
                    input{
                        width: 100px;
                    }
                }
            }
            .del{
                width: 20px;
                height: 120px;
                position: relative;
                span{
                    position: absolute;
                    top: 5px;
                    right: 0;
                    color: #ff0000;
                    font-weight: bold;
                    font-size: 25px;
                }
            }
        }
    }
    .btn{
        position: fixed;
        bottom: 55px;
        left: 0;
        width: 100%;
        height: 50px;
        line-height: 40px;
        box-sizing: border-box;
        padding: 5px 5%;
        background-color: #fff;
        display: flex;
        justify-content: space-between;
        button{
            width:80px;
            border: 0;
            border-radius: 25px;
            background-color: #ff0101;
            color: #fff;
        }
        .count{
            font-weight: bold;
            .price{
                color: #ff0000;
                font-size: 20px;
            }
        }
    }
}
</style>
<template>
    <div class="cart">
        <navigator
            :title="'购物车'"
        />
        <div class="list">
            <div class="item" v-for="(item,index) in cartList" :key="item.ID">
                <div class="radio">
                    <input type="checkbox" v-model="item.checked" @change="danChange()" />
                </div>
                <div class="headImg">
                    <img :src="item.pro_img" alt="">
                </div>
                <div class="detail">
                    <div class="title">
                        {{ item.pro_title }}
                    </div>
                    <div class="sku">
                        <span class="sku_item">
                            <i v-for="(itemes,index) in item.pro_sku" :key="index">
                                {{itemes.desc}}
                            </i>
                        </span>
                        <span class="price">
                            ✖ {{ item.pro_price }}
                        </span>
                    </div>
                    <div class="tabbar">
                        <button :disabled="item.pro_num <= 1" @click="del(index)">
                            ➖
                        </button>
                        <input type="number" v-model="item.pro_num" readonly>
                        <button @click="add(index)">
                            ➕
                        </button>
                    </div>
                </div>
                <div class="del">
                    <span @click="delCart(item.ID)">
                        ×
                    </span>
                </div>
            </div>
        </div>
        <div class="btn">
            <input type="checkbox" v-model="allCheck" @change="allChange()">
            <span class="count">
                结算:
                <span class="price">
                    ￥{{ countPrice }}
                </span>
            </span>
            <button>
                结算
            </button>
        </div>
    </div>
</template>
<script>
    // 引入接口
    import {
        getCartList,
        delCartNum,
        editCartNum
    } from '@/api'
    // 引入导航组件
    import navigator from '@/components/navigator.vue';
    export default {
        methods :{
            editCart(address_id,number){
                editCartNum({
                    address_id,
                    number
                }).then(res => {
                    if(res.data.code == 1){
                        this.$toast('修改成功')
                    }
                })
            },
            add(index){
                // 让相应的购物车数量增加
                this.cartList[index].pro_num++;
                this.editCart(
                    this.cartList[index].ID,
                    this.cartList[index].pro_num
                )
            },
            del(index){
                // 让相应的购物车数量减少
                this.cartList[index].pro_num--
                this.editCart(
                    this.cartList[index].ID,
                    this.cartList[index].pro_num
                )
            },
            delCart(address_id){
                this.$dialog({
                    title: '提示',
                    message: '删除后不可恢复，确定删除吗',
                    showCancelButton: true
                }).then(res => {
                    // 删除购物车
                    delCartNum({address_id}).then(res => {
                        if(res.data.code == 1){
                            this.$toast('删除成功');
                            // 重新获取购物车列表 保证页面数据是最新的
                            this.getList();
                        }
                    })
                }).catch(err => {
                    console.log(err);
                })
            },
            danChange(){
                // 记录总共选择了多少个
                let count = 0;
                this.cartList.forEach(item => {
                    // 如果当前选中此数据 
                    if(item.checked == true){
                        // 选中+1
                        count++
                    }
                })
                // 如果选中的数量等于 数据的长度
                if(count === this.cartList.length){
                    this.allCheck = true;
                }else{
                    this.allCheck = false;
                }
            },
            allChange(){
                // 轮询
                this.cartList.forEach(item => {
                    item.checked = this.allCheck;
                })
            },
            getList(){
                getCartList().then(res => {
                    console.log(res.data);
                    if(res.data.code == 1){
                        this.cartList = res.data.result.map(item => {
                            item.checked = false;
                            item.pro_sku = JSON.parse(JSON.parse(item.pro_sku.replace(/'/g,'')))
                            return item
                        })
                    }
                })
            }
        },
        // 计算属性
        computed: {
            countPrice(){
                // 初始化总价格
                let count = 0;
                // 轮询数据
                this.cartList.forEach(item => {
                    if(item.checked === true){
                        count += item.pro_price * item.pro_num
                    }
                })
                // 返回计算结果
                return count
            }
        },
        created(){
            this.getList()
        },
        data() {
            return {
                cartList:[],
                allCheck: false
            }
        },
        components: {
            navigator
        }
    }
</script>